package com.example.myapplication.part13;

import android.annotation.SuppressLint;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class Main3Activity extends AppCompatActivity {
        private RecyclerView recyclerView;
        private AppListAdapter adapter;
        private List<AppInfo> appList = new ArrayList<>();

        @SuppressLint("MissingInflatedId")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main3);

            recyclerView = findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));

            loadInstalledApps();
        }

        private void loadInstalledApps() {
            PackageManager pm = getPackageManager();
            List<ApplicationInfo> apps = pm.getInstalledApplications(0);

            for (ApplicationInfo appInfo : apps) {
                String appName = pm.getApplicationLabel(appInfo).toString();
                Drawable icon = ((PackageManager) pm).getApplicationIcon(appInfo);
                String packageName = appInfo.packageName;
                this.appList.add(new AppInfo(packageName, appName, icon));
            }

            adapter = new AppListAdapter(this, appList);
            recyclerView.setAdapter(adapter);
        }
    }